package com.lxk.service.impl;

import com.lxk.dao.AdminMapper;
import com.lxk.pojo.Admin;
import com.lxk.pojo.AdminExample;
import com.lxk.service.LoginService;
import com.lxk.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author JohnSoybean
 * @create 2020/3/19 18:07
 * @description
 */
@Service
@Transactional
public class LoginServiceImpl implements LoginService {
    @Autowired
    private AdminMapper am;

    public int checkLogin(String adminName, String pwdMd5) {
        AdminExample example = new AdminExample();
        AdminExample.Criteria criteria = example.createCriteria();
        criteria.andAdminNameEqualTo(adminName);
        if(am.countByExample(example)==0) {
            return -1;
        }else{
            Admin admin = am.selectByExample(example).get(0);
            String pwdData = admin.getPwd();
            String pwd = MD5Util.string2MD5(pwdData);
            if(pwdMd5.equals(pwd)) {
                return 1;
            }else {
                return 0;
            }
        }
    }

    public int addAdmin(String adminName, String pwd, Integer authType) {
        Admin admin = new Admin();
        admin.setAdminName(adminName);
        admin.setPwd(pwd);
        admin.setAuthType(authType);
        return am.insertSelective(admin);
    }

    public int deleteAdmin(Integer adminId) {
        return am.deleteByPrimaryKey(adminId);
    }

    public int updateAdmin(Admin admin) {
        return am.updateByPrimaryKey(admin);
    }

    public Admin getAdminInfo(Integer adminId) {
        Admin admin = am.selectByPrimaryKey(adminId);
        return admin;
    }

    public Admin getAdminInfo(String adminName) {
        AdminExample example = new AdminExample();
        AdminExample.Criteria criteria = example.createCriteria();
        criteria.andAdminNameEqualTo(adminName);
        Admin admin = am.selectByExample(example).get(0);
        return admin;
    }

    public String getPwd(Integer adminId) {
        Admin admin = am.selectByPrimaryKey(adminId);
        return admin.getPwd();
    }

    public int changePwd(Integer adminId, String pwd) {
        Admin admin = new Admin();
        admin.setAdminId(adminId);
        admin.setPwd(pwd);
        return am.updateByPrimaryKeySelective(admin);
    }
}
